﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using ZhiHeng.OneCard.WpfClient.Common; 
using System;

namespace ZhiHeng.OneCard.WpfClient.ViewModel
{
    public class CatalogViewModel : ViewModelBase
    {
      
        private ObservableCollection<Goods> _goodDataItems;
        public ObservableCollection<Goods> GoodDataItems
        {
            get
            {

                return _goodDataItems;
            }
            set
            {
                _goodDataItems = value;
                this.RaisePropertyChanged("GoodDataItems");
            }
        }
        private Goods _SelectGoods;
        /// <summary>
        ///  SelectGoods
        /// </summary>
        public Goods SelectGoods
        {
            get { return _SelectGoods; }
            set
            {
                _SelectGoods = value;
                this.RaisePropertyChanged("SelectGoods");
            }
        }

        private Common.CheckboxDataItem _selectedGoodType;
        public Common.CheckboxDataItem SelectedGoodType
        {
            get
            {
                if (_selectedGoodType == null)
                    return _selectedGoodType;
                var GoodtypeStr = "";
                foreach (var item in GoodTypeItems)
                {
                    if (item.IsEnabled)
                        GoodtypeStr += item.Type.ToString() + ",";
                }
                this.GoodDataItems = GetGoods(GoodtypeStr, UserInfo.MercAcc ?? 0);
               
                return _selectedGoodType;
            }
            set
            {
                _selectedGoodType = value;
                RaisePropertyChanged("SelectedGoodType");
            }
        }
        /// <summary>
        /// 获取所销售商品
        /// </summary>
        /// <param name="MerAcc">商户ID</param>
        /// <param name="GoodType">商品类型</param>
        /// <returns></returns>
        private ObservableCollection<Goods> GetGoods(string GoodType,int MercAcc)
        {
            var goods1 = WcfOneCardActionService.QueryCatalog(GoodType,MercAcc);
            var retGood = new ObservableCollection<Goods>();
            foreach (var item in goods1)
            {
                var good = new Goods();
                good.Id = item.Id;
                good.Code = item.Code;
                good.Good1 = item.Good;
                good.Name = item.Name;
                good.Price = item.Price;
                good.Unit = item.Unit;
                good.GoodType = item.GoodType;
                good.Inventory = item.MInventory;
                retGood.Add(good);
            }
            return retGood;
        }
       
        #region 商品销售范围绑定
        private ObservableCollection<Common.CheckboxDataItem> _goodTypeItems = new ObservableCollection<Common.CheckboxDataItem>();
        /// <summary>
        ///  GoodTypeItems
        /// </summary>
        public ObservableCollection<Common.CheckboxDataItem> GoodTypeItems
        {
            get
            {
                if (this._goodTypeItems.Count == 0)
                {
                    this._goodTypeItems = GetMerGoodType(UserInfo.MercAcc??0);
                }
                return _goodTypeItems;
            }
            set
            {
                _goodTypeItems = value;
                this.RaisePropertyChanged("GoodTypeItems");
            }
        }
        /// <summary>
        /// 获取商户销售范围
        /// </summary>
        /// <param name="MerAcc">商户ID</param>
        /// <returns></returns>
        private ObservableCollection<Common.CheckboxDataItem> GetMerGoodType(int MerAcc)
        {
            var GoodType = WcfOneCardActionService.GetMercaccScope(MerAcc);
            var CheckboxDataItem = new ObservableCollection<Common.CheckboxDataItem>();
            foreach (var item in GoodType)
            {
                var goodtypeItem = new Common.CheckboxDataItem();
                goodtypeItem.Name = item.Name;
                goodtypeItem.Type = item.Code;
                CheckboxDataItem.Add(goodtypeItem);
            }
            return CheckboxDataItem;

        }
        #endregion
         public CatalogViewModel()
            
        {

        }
        public DelegateCommand OkCommand
        {
            get
            {
                return MainViewModel.Instance.ReturnToStartCommand;
            }
        }
        DelegateCommand _storageCommand;
        public DelegateCommand StroageCommand
        {
            get {
                return _storageCommand ?? (this._storageCommand = new DelegateCommand(()=>
                {
                    try
                    {
                        int i = WcfOneCardActionService.Storage(UserInfo.MercAcc ?? 0, SelectGoods.Id, SelectGoods.Code, SelectGoods.Inventory??0);
                        if (i > 0)
                            ShowMsg("进货入库成功");
                        else
                            ShowMsg("进货入库失败", false);
                    }
                    catch (Exception ex)
                    {
                        ShowMsg("进货入库失败Error:"+ex.Message.ToString(), false);

                    }
                       
                }));
            
            }
        }
    }
    public class Goods:ZhiHeng.Consume.DataContract.good
    {
    }
}
